package view.component.weapon
{
	import flash.text.TextFormat;
	
	import enums.main.UISize;
	
	import feathers.controls.Button;
	import feathers.controls.Label;
	import feathers.controls.List;
	import feathers.controls.Screen;
	import feathers.controls.Scroller;
	import feathers.controls.renderers.DefaultListItemRenderer;
	import feathers.controls.renderers.IListItemRenderer;
	import feathers.data.ListCollection;
	import feathers.layout.HorizontalLayout;
	import feathers.layout.TiledRowsLayout;
	import feathers.layout.VerticalLayout;
	
	import model.vo.weapon.BaseWeaponVo;
	
	import starling.animation.Tween;
	import starling.core.Starling;
	import starling.display.Image;
	import starling.events.Event;
	import starling.utils.Color;
	
	import util.WeaponUtil;
	
	import view.event.weapon.SearchResultListViewEvent;
	
	
	
	/**
	 * 武器搜索结果列表
	 * @author £iang
	 * 
	 */	
	public class SearchResultListView extends Screen
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** 武器列表 */
		public var weaponList:List;
		/** 无查询结果标签 */
		public var noResultLabel:Label;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		/** 列表高度 */
		private var _viewH:Number;
		/** 武器名 */
		private var _weaponName:String;
		/** weaponList数据 */
		private var _listDataWeapon:ListCollection;
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化函数 
		 * 
		 */		
		override protected function initialize():void
		{
			noResultLabel = new Label();
			noResultLabel.text = "无查询结果";
			noResultLabel.name = "centerContentLabel";
			this.addChild( noResultLabel );
			
			var tileLayout:TiledRowsLayout = new TiledRowsLayout();
			tileLayout.useSquareTiles = false; 
			tileLayout.tileVerticalAlign = VerticalLayout.VERTICAL_ALIGN_TOP;
			tileLayout.tileHorizontalAlign = HorizontalLayout.HORIZONTAL_ALIGN_LEFT;
			tileLayout.verticalAlign = VerticalLayout.VERTICAL_ALIGN_TOP;
			tileLayout.horizontalAlign = HorizontalLayout.HORIZONTAL_ALIGN_LEFT;
			tileLayout.gap = 0;
			
			weaponList = new List();
			this.addChild( weaponList );
			weaponList.width = UISize.MAIN_W;
			weaponList.itemRendererProperties.labelField  = "label";
			weaponList.itemRendererProperties.iconField = "icon";
			weaponList.itemRendererFactory = itemRenderFactroy;
			weaponList.scrollerProperties.scrollBarDisplayMode = Scroller.SCROLL_BAR_DISPLAY_MODE_NONE;
			weaponList.height = _viewH;
			weaponList.layout = tileLayout;
			
		}
		
		/**
		 * 绘制函数 
		 * 
		 */		
		override protected function draw():void
		{
			noResultLabel.width = UISize.MAIN_W;
		}
		
		/**
		 * produceItem皮肤 
		 * @return 
		 * 
		 */		
		private function itemRenderFactroy():IListItemRenderer
		{
			/*var renderer:DefaultListItemRenderer = FG2FUGenerator.generateRoundRectItem(
				UISize.MAIN_W / 2 / this.dpiScale, 
				100, this.dpiScale, 12, "small"
			);*/
			var renderer:DefaultListItemRenderer = new DefaultListItemRenderer();
			renderer.width = UISize.MAIN_W / 2;
			renderer.height = 100 * this.dpiScale;
			renderer.defaultLabelProperties.textFormat = new TextFormat( "微软雅黑,宋体", 24 * this.dpiScale, 
				Color.WHITE, false );
			renderer.horizontalAlign = Button.HORIZONTAL_ALIGN_LEFT;
			renderer.verticalAlign = Button.VERTICAL_ALIGN_MIDDLE;
			renderer.paddingLeft = 15 * this.dpiScale;
			renderer.itemHasAccessory = false;
			renderer.gap = 10 * this.dpiScale;
			renderer.defaultLabelProperties.wordWrap = true;
			renderer.defaultLabelProperties.isHTML = true;
			renderer.addEventListener( Event.TRIGGERED, itemTriggeredHandler );
			return renderer;
		}
		
		/**
		 * 设置列表数据 
		 * @param data 数据
		 * 
		 */		
		public function setData( data:Vector.<BaseWeaponVo> ):void
		{
			var len:int = data.length;
			var i:int;
			
			this.alpha = 0;
			disposeItems();
			_listDataWeapon = new ListCollection([]);
			
			if ( len == 0 )
			{
				noResultLabel.visible = true;
			}
			else
			{
				noResultLabel.visible = false;
				
				for ( i = 0; i < len; i ++ )
				{
					_listDataWeapon.push( WeaponUtil.mapWeaponVo2ListObj( data[ i ] as BaseWeaponVo, _weaponName ) );
				}
			}
			
			weaponList.dataProvider = _listDataWeapon;
			
			var tween:Tween = new Tween( this, 0.4 );
			
			tween.animate( "alpha", 1 );
			Starling.juggler.add( tween );
		}
		
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 * 处理item被点击后派发的事件 
		 * @param event item被点击后派发的事件 
		 * 
		 */		
		protected function itemTriggeredHandler( event:starling.events.Event ):void
		{
			var searchResultListEvent:SearchResultListViewEvent = 
				new SearchResultListViewEvent( 
					SearchResultListViewEvent.DETAIL
				);
			
			searchResultListEvent.weaponId =  _listDataWeapon.data[ ( event.target as DefaultListItemRenderer ).index ].weaponId;
			this.parent.parent.parent.dispatchCommonEvent( searchResultListEvent );
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		
		/**
		 * 析构函数 
		 * 
		 */		
		override public function dispose():void
		{
			disposeItems( true ); 
			weaponList.dispose();
			weaponList = null;
			
			noResultLabel.dispose();
			noResultLabel = null;
			super.dispose();
		}
		
		/**
		 * 析构列表中的item 
		 * 
		 */		
		public function disposeItems( isDeep:Boolean = false ):void
		{
			var i:int;
			var len:int;
			
			if ( _listDataWeapon )
			{
				len = _listDataWeapon.length;
				
				for ( i = 0; i < len; i ++ )
				{
					( _listDataWeapon.data[ i ].icon as Image ).dispose();
					( _listDataWeapon.data[ i ].icon as Image ).texture.dispose();
					_listDataWeapon.data[ i ].icon = null; 
					_listDataWeapon.data[ i ] = null;
				}
				
				_listDataWeapon.removeAll();
				_listDataWeapon = null;
			}
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function SearchResultListView( h:Number, name:String )
		{
			super();
			_viewH = h;
			_weaponName = name;
		}
	}
}